var fs = require('fs')
var express = require('express')
let template = require('art-template');
const jwt = require("jsonwebtoken")

const { resourceUsage } = require('process')
var playRouter = express.Router()
var axios = require("axios")

playRouter.get('/play',function(req,res){

    res.render('front-stage/l-play.html', { userinfo: '' })
    var adVideo;
    if (req.session.token == undefined){
        axios({        
            url: "video/advertisement/findAllAdver",
            method: "get",
            headers: {
                token: req.session.token ? req.session.token : ""
            },
        })
            .then(function (data) {
               adVideo=data.data.data
               axios({
                url: "video/order/findAllVip",
                method: "post",
            })
                .then(function (data) {
                    axios({
                        url: "video/order/findAllVip",
                        method: "post",
                    })
                    .then(function (data) {
                        let allonevip = data.data.object 
                        res.render('front-stage/l-play.html', { userinfo: '', adVideo:adVideo,allonevip:allonevip})
 
                })
                .catch(function (error) {
                    console.log(error)
                    res.status(500).json({
                        message: '黄金vip遍历失败'
                    })
                })


            })
            .catch(function (err) {
                console.log('遍历失败')
            })
            })
    }else{

        axios({        
            url: "video/advertisement/findAllAdver",
            method: "get",
            headers: {
                token: req.session.token ? req.session.token : ""
            },
        })
            .then(function (data) {
               adVideo=data.data.data

               axios({
                url: "video/order/findAllVip",
                method: "post",
            })
                .then(function (data) {
                    let allonevip = data.data.object 

                    axios({
                        url: "video/user/findUserById",
                        params: {
                            userId: jwt.decode(req.session.token).userId,
                        },
                        method: "get",
                    })
                        .then(function (data) {
                            let viptype = data.data.object.vip;
                            if (viptype == "0") { 
                                res.render('front-stage/l-play.html',{userinfo:jwt.decode(req.session.token),allonevip:allonevip})
                            } else if (viptype == "1") {
                                res.render('front-stage/l-play.html',{userinfo:jwt.decode(req.session.token),allonevip:allonevip})
                            } else {   
                                res.render('front-stage/l-play.html', {userinfo: jwt.decode(req.session.token), allonevip: allonevip})
                            }
                        })
                        .catch(function (error) {
                            
                            res.status(500).json({
                                message: '获取用户信息失败'
                            })
                        })


                })
                .catch(function (error) {
                   
                    res.status(500).json({
                        message: '黄金vip遍历失败'
                    })
                })


            })
            .catch(function (err) {
                console.log('遍历失败')
            })
    }
})
// 1.遍历主评论
playRouter.get('/video/findCommentByVideoId', function (req, res) {  
    axios({        
        url: "video/findCommentByVideoId",
        method: "get",
        headers: {
            token: req.session.token ? req.session.token : ""
        },
        params:{
            videoId:req.query.videoId
        }
    })
        .then(function (data) {
            res.status(200).json({              
                data:data.data.object
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})

// 2.遍历子评论
playRouter.get('/video/findCommentSonByCommentId', function (req, res) { 
    axios({        
        url: "video/findCommentSonByCommentId",
        method: "get",
        headers: {
            token: req.session.token ? req.session.token : ""
        },
        params:{
            commentId:req.query.commentId
        }
    })
        .then(function (data) {
            res.status(200).json({              
                data:data.data.object
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})


// 3.判断是否点过赞视频
playRouter.post('/video/JudgeThumbsUp', function (req, res) { 
    console.log(req.query)
    axios({        
        url: "video/JudgeThumbsUpVideo",
        method: "post",
        params:{
            userId:req.query.userId,
            videoId:req.query.videoId
        }
    })
        .then(function (data) {
            res.status(200).json({              
                data:data.data
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})
//4.点赞视频
playRouter.post('/video/ThumbsUp', function (req, res) { 
    console.log(req.query)
    axios({        
        url: "video/ThumbsUpVideo",
        method: "post",
        params:{
            userId:req.query.userId,
            videoId:req.query.videoId
        }
    })
        .then(function (data) {
            res.status(200).json({              
                data:data.data
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})

// 4.用户进行评论
playRouter.post('/video/addComment', function (req, res) { 
    console.log(req.query)
    axios({        
        url: "video/addComment",
        method: "post",
        params:{
            content:req.query.content,
            userId:req.query.userId,
            videoId:req.query.videoId
        }
    })
        .then(function (data) {
            res.status(200).json({              
                data:data.data.object
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})
// 5.用户进行子评论
playRouter.post('/video/addCommentSon', function (req, res) { 
    console.log(req.query)
    axios({        
        url: "video/addCommentSon",
        method: "post",
        params:{
            content:req.query.content,
            userOneId:req.query.userOneId,
            userTwoId:req.query.userTwoId,
            commentId:req.query.commentId
        }
    })
        .then(function (data) {
            res.status(200).json({              
                data:data.data.object
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})


// 6.对主评论进行点赞
// 判断我是否给主评论点过赞
playRouter.post('/video/JudgeThumbsUpComment', function (req, res) { 
    axios({        
        url: "video/JudgeThumbsUpComment",
        method: "post",
        params:{
            commentId:req.query.commentId,
            commentType:req.query.commentType,
            userId:req.query.userId
        }
    })
        .then(function (data) {
            res.status(200).json({              
                data:data.data
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})
// 对主评论进行点赞
playRouter.post('/video/ThumbsUpComment', function (req, res) { 
    console.log(req.query)
    axios({        
        url: "video/ThumbsUpComment",
        method: "post",
        params:{
            commentId:req.query.commentId,
            commentType:req.query.commentType,
            userId:req.query.userId
        }
    })
        .then(function (data) {
            res.status(200).json({              
                data:data.data
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})
// 7.对子评论进行点赞
// 8.收藏
//判断是否收藏
playRouter.post('/video/JudgeCollection', function (req, res) { 
    console.log(req.query)
    axios({        
        url: "video/JudgeCollection",
        method: "post",
        params:{
            videoId:req.query.videoId,
            userId:req.query.userId
        }
    })
        .then(function (data) {
            res.status(200).json({              
                // data:data.data.object1,
               data:data.data
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})
// 进行收藏
playRouter.post('/video/Collection', function (req, res) { 
    axios({        
        url: "video/Collection",
        method: "post",
        params:{
            videoId:req.query.videoId,
            userId:req.query.userId,
            collectionFoldersId:req.query.collectionFoldersId
        }
    })
        .then(function (data) {
          
            res.status(200).json({              
                data:data.data.object
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})

// 遍历某用户的收藏夹
playRouter.get('/video/findAllCollectionFolders', function (req, res) { 
    axios({        
        url: "video/findAllCollectionFolders",
        method: "get",
        params:{
            userId:req.query.userId,   
        }
    })
        .then(function (data) {
            res.status(200).json({              
                data:data.data.object
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})

// 建立收藏夹功能
playRouter.post('/video/addCollectionFolder', function (req, res) { 
    axios({        
        url: "video/addCollectionFolder",
        method: "post",
        params:{
            collectionFoldersName:req.query.collectionFoldersName,
            userId:req.query.userId    
        }
    })
        .then(function (data) {
            res.status(200).json({              
                data:data.data.object
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})


// 遍历某个视频下的热度
playRouter.get('/video/Doh', function (req, res) { 
    axios({        
        url: "video/Doh",
        method: "get",
        params:{
           videoId:req.query.videoId,
            userId:req.query.userId    
        }
    })
        .then(function (data) {
            res.status(200).json({              
                data:data.data.object
            })
        })
        .catch(function (err) {
            res.status(500).json({
                message: '查找失败'
            })
        })
})

// 遍历出来的广告
playRouter.get('/advertisement/findAllAdver', function (req, res) { 
    axios({        
        url: "video/advertisement/findAllAdver",
        method: "get"
    })
    .then(function (data) { 
        res.status(200).json({              
            data:data.data.data
        })
    })
    .catch(function (err) {
        res.status(500).json({
            message: '查找失败'
        })
    })
})



/*退出登录！！！ */
playRouter.post('/logout', function (req, res) {
    axios({
        url: "video/user/logout",
        method: "post",
    })
        .then(function (data) {
          
            req.session.token = null;
            // res.redirect('/myperson')
            res.status(200).json({
                message: '退出登录成功'
                
            })
        })
        .catch(function (error) {
          
            res.status(500).json({
                message: '退出登录失败'
            })
        })
})


// 根据电视剧id查询电视剧和链接
playRouter.get('/video/tv/findTVMsgByTVId', function (req, res) {
    axios({
        url: "video/tv/findTVMsgByTVId",
        method: "get",
        params: {
            tvId: req.query.tvId
        }
    })
        .then(function (data) {
            res.status(200).json({ 
                data: data.data.data,
            })
        })
        .catch(function (error) {
            res.status(500).json({
               message:'失败'
            })
        })
})

// 电视剧的下一集
playRouter.get('/tv/lastTvSrcByNowSetNumber', function (req, res) {
    axios({
        url: "video/tv/lastTvSrcByNowSetNumber",
        method: "get",
        params: {
            tvId: req.query.tvId,
            nowSetNumber:req.query.nowSetNumber
        }
    })
        .then(function (data) {
            res.status(200).json({ 
                data: data.data.data,
            })
        })
        .catch(function (error) {
            res.status(500).json({
               message:'失败'
            })
        })
})

// 获取用户信息
playRouter.get('/user/findUserById', function (req, res) {
    axios({
        url: "video/user/findUserById",
        method: "get",
        params: {
           userId:req.query.userId
        }
    })
        .then(function (data) {
            res.status(200).json({ 
                data: data.data,
            })
        })
        .catch(function (error) {
            res.status(500).json({
               message:'失败'
            })
        })
})

// 获取演元信息
playRouter.get('/actor/findAllActorsByVideoId', function (req, res) {
    axios({
        url: "video/actor/findAllActorsByVideoId",
        method: "get",
        params: {
           videoId:req.query.videoId
        }
    })
        .then(function (data) {
            res.status(200).json({ 
                data: data.data.data,
            })
        })
        .catch(function (error) {
            res.status(500).json({
               message:'失败'
            })
        })
})

//遍历出来几个电视剧的名字
playRouter.get('/index/MoreTV', function (req, res) {
    axios({
        url: "video/index/MoreTV",
        method: "get"
    })
    .then(function (data) {
        res.status(200).json({ 
            data: data.data.data,
        })
    })
    .catch(function (error) {
        res.status(500).json({
            message:'失败'
        })
    })
})

// 举报评论
playRouter.post('/addReportCommentRecord', function (req, res) {
    axios({
        url: "video/addReportCommentRecord",
        method: "post",
        params:{
            commentId:req.query.commentId,
            commentType:req.query.commentType,
            content:req.query.content,
            userId:req.query.userId
        }

    })
    .then(function (data) {
        res.status(200).json({ 
            data: data.data,
        })
    })
    .catch(function (error) {
        console.log(581)
        res.status(500).json({
            message:'失败'
        })
    })
})

// 删除主评论
playRouter.delete('/deleteComment', function (req, res) {
    axios({
        url: "video/deleteComment",
        method: "delete",
        params:{
            commentId:req.query.commentId,
            userId:req.query.userId
        }
    })
    .then(function (data) {
        res.status(200).json({ 
            data: data.data,
        })
    })
    .catch(function (error) {
        console.log(581)
        res.status(500).json({
            message:'失败'
        })
    })
})

// 删除子评论
playRouter.delete('/deleteCommentSonByUserOne', function (req, res) {
    axios({
        url: "video/deleteCommentSonByUserOne",
        method: "delete",
        params:{
            commentSonId:req.query.commentSonId ,
            userOneId:req.query.userOneId 
        }
    })
    .then(function (data) {
        res.status(200).json({ 
            data: data.data,
        })
    })
    .catch(function (error) {
        console.log(581)
        res.status(500).json({
            message:'失败'
        })
    })
})

// 下载该视频
playRouter.get('/videoDownLoad', function (req, res) {
    axios({
        url: "video/downLoad/videoDownLoad",
        method: "get",
        params:{
            filePath:req.query.filePath,
            url:req.query.url,
            videoId:req.query.videoId
        }
    })
    .then(function (data) {
        res.status(200).json({ 
            data: data.data,
        })
    })
    .catch(function (error) {
        res.status(500).json({
            message:'失败'
        })
    })
})
module.exports = playRouter